package com.undsf.fgo.crawler

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

class Servant {
    def id
    def name
    def avatar
}

int run(String url) throws IOException {
    int counter = 0
    OkHttpClient client = new OkHttpClient()
    Request request = new Request.Builder()
            .url(url)
            .build()
    Response response = client.newCall(request).execute()
    String html = response.body().string()

    Document doc = Jsoup.parse(html)
    def tabs = doc.select(".tabbertab")
    for (def tab : tabs) {
        def tbody = tab.select("tbody").get(0)
        def records = tbody.children()
        records = records.subList(1, records.size())
        for (def record : records) {
            def id = record.child(3).text()
            if (id.contains("Null")) continue
            Servant servant = new Servant()
            servant.id = id
            servant.name = record.child(1).text()
            counter++
        }
    }

    return counter
}

String indexUrl = "http://fategrandorder.wikia.com/wiki/Servant_List"
indexUrl = "http://wikia.fgorp.vdm/wiki/Servant_List.html"

println("获取到" + run(indexUrl) + "个节点信息")
